package com.he.hsdyc.controller;

import com.he.hsdyc.common.utils.DateUtil;
import com.he.hsdyc.common.utils.ValidateUtils;
import com.he.hsdyc.config.wechatmini.WxPushService;
import com.he.hsdyc.config.wechatmini.model.TemplateData;
import com.he.hsdyc.model.msg.ErrorCode;
import com.he.hsdyc.model.msg.Message;
import com.he.hsdyc.model.vo.CourseOrderVo;
import com.he.hsdyc.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


@Api(value="订单设置controller",description="订单操作",tags={"订单操作接口"})
@RestController
@RequestMapping(value = "order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private WxPushService wxPushService;


    /**
     * 功能描述: 课程订单列表
     * @param:
     */
    @ApiOperation("课程订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "schoolId", value = "学校id", dataType = "long", required = true, paramType = "query"),
            @ApiImplicitParam(name = "orderType", value = "种类（0新单/1续费/2预付款/3定金）", dataType = "int", required = false, paramType = "query"),
            @ApiImplicitParam(name = "status", value = "订单状态", dataType = "int", required = false, paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "Stirng", required = false, paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "pageNo", value = "页数", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pagesize", value = "条数", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "stuName", value = "学生姓名", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "code", value = "订单编号", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "courseName", value = "课程名称", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "payType", value = "支付方式", dataType = "int", required = false, paramType = "query"),
            @ApiImplicitParam(name = "payStartTime", value = "开始时间", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "payEndTime", value = "结束时间", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "del", value = "是否删除", dataType = "Boolean", required = true, paramType = "query")
    })
    @RequestMapping(value = "listback",method = RequestMethod.POST)
    public Message<Map<String,Object>> listback(@RequestBody CourseOrderVo vo) {
        if(ValidateUtils.isIllegalLong(vo.getSchoolId())||ValidateUtils.isIllegalInt(vo.getPageNo())||ValidateUtils.isIllegalInt(vo.getPageSize()) || vo.getDel() == null)
            return new Message(ErrorCode.ERROR_ARG);

        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(() -> {

        });
        return new Message(ErrorCode.SUCCESS,orderService.listback(vo));
    }


    /**
     * 功能描述: 订单统计
     */
    @ApiOperation("订单统计")
    @RequestMapping(value = "countOrder",method = RequestMethod.POST)
    @ApiImplicitParam(name = "schoolId", value = "学校id", dataType = "long", required = true, paramType = "query")
    public Message<Map<String,Object>> countOrder(@RequestBody CourseOrderVo vo) {
        if(ValidateUtils.isIllegalLong(vo.getSchoolId()))
            return new Message<>(ErrorCode.ERROR_ARG);

        long startTime = System.currentTimeMillis();
        Map<String, Object> map = orderService.countOrder(vo);
        long endTime = System.currentTimeMillis();
        System.out.println("单线程任务执行时间:====================="+(endTime-startTime));
        return new Message<>(ErrorCode.SUCCESS,map);
    }


    @RequestMapping(value = "push",method = RequestMethod.POST)
    public Message push() {
        Map<String, TemplateData> data = new HashMap<>();
               //拼接模板信息
                data.put("keyword1", new TemplateData("张三", "#173177"));
                data.put("keyword2", new TemplateData("20.01", "#173177"));
                data.put("keyword3", new TemplateData("1.00", "#173177"));
                data.put("keyword4", new TemplateData(DateUtil.getNowTime(), "#173177"));
                data.put("remark", new TemplateData("123456", "#173177"));
        String message = wxPushService.pushMessage("parent", "oOXIF5u7v2ysypXv7EekVwKsTBUs", "pPiU617W9s8y7_m4k8h1DfBuAk09bxkT1OVrhthbymc", data);
        return new Message<>(ErrorCode.SUCCESS,message);
    }
}
